<?php

namespace App\Api\Controllers\Glow;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Store;
use Illuminate\Support\Facades\Validator;
use App\Models\Merchant;
use App\Models\MerchantStore;

class UserController extends BaseController{
    
    // 代理商小程序 门店管理
    public function stores_list(Request $request){
        $pid = $request->get('pid', ''); // 门店id
        $store_name = $request->get('store_name', '');//关键词
        
        if(empty($pid)){
            $this->status = 2;
            $this->message = '缺少参数门店id, 请刷新重试';
            return $this->format();
        }
        
        $store = DB::table('stores')->where(['id' => $pid])->first();
        $p_stores = DB::table('stores')->where('pid', $store->id)->select('store_id')->get(); // 查找总店下的分店
        if( !empty( $p_stores[0]->store_id ) ){ // 合并总店和分店
            $store_ids[0] = $store->store_id;
            $p_stores = json_decode($p_stores, true);
            foreach($p_stores as $k1 => $v1){
                $store_ids[] = $v1['store_id'];
            }
        }else{
            $store_ids[0] = $store->store_id;
        }
        
        $where = [];
        $where[] = ['is_delete', '=', 0];
        $where[] = ['is_close', '=', 0];
        
        if ($store_name) {
            $where[] = ['store_name', 'like', '%' . $store_name . '%'];
        }
        $data = DB::table('stores')->where($where)->whereIn('store_id', $store_ids)->select('store_id', 'store_name', 'pid', 'province_name', 'city_name', 'store_address')->orderBy('updated_at', 'desc')->get();

        $this->status = 1;
        $this->message = '数据返回成功';
        return $this->format($data);
    }
    
    // 代理商小程序 添加收银员
    public function create_merchant(Request $request){
        try {
            $store_id = $request->get('store_id', '');
            $type = $request->get('type', '2');
            $name = $request->get('name', '');
            $phone = $request->get('phone', '');
            $data = $request->except(['token']);
            $password = $request->get('password', '');
            
            $store = Store::where('store_id', $store_id)->first();
            if ($password == "") {
                $password = '000000';
            }
            if ($name == "") {
                $this->status = 2;
                $this->message = '姓名必填';
                return $this->format();
            }
            if ($phone == "") {
                $this->status = 2;
                $this->message = '手机号必填';
                return $this->format();
            }
            if (!$store) {
                $this->status = 2;
                $this->message = '门店不存在';
                return $this->format();
            }
            $rules = [
                'phone' => 'required|min:11|max:11',
            ];
            $validator = Validator::make($data, $rules);
            if ($validator->fails()) {
                return json_encode([
                    'status' => 2,
                    'message' => '手机号位数不正确',
                ]);
            }
            //验证密码
            if (strlen($password) < 6) {
                return json_encode([
                    'status' => 2,
                    'message' => '密码长度不符合要求'
                ]);
            }
            $rules = [
                'phone' => 'required|unique:merchants',
            ];
            $validator = Validator::make($data, $rules);
            if ($validator->fails()) {
                return json_encode([
                    'status' => 2,
                    'message' => '手机号已经注册'
                ]);
            } else {
                $dataIN = [
                    'pid' => $store->merchant_id ? $store->merchant_id : "0",
                    'type' => $type,
                    'name' => $name,
                    'email' => '',
                    'password' => bcrypt($password),
                    'phone' => $phone,
                    'user_id' => $store->user_id,//推广员id
                    'config_id' => $store->config_id,
                    'wx_openid' => ''
                ];
                
                $insert = Merchant::create($dataIN);
                $merchant_id = $insert->id;
            }
            
            MerchantStore::create([
                'store_id' => $store_id,
                'merchant_id' => $merchant_id,
            ]);
            
            $this->status = 1;
            $this->message = '收银员添加成功';
            return $this->format();

        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }
    
    
}